 // 初始化组件
 var CategoryMapping = {};
 var GlobalCommand = {};
 var bgDefault = $("#bgImages").attr("rel-url");

 var Schema = {
    config: {markerSize: 14},
    pageDefaults: {
        backgroundColor: "transparent",
        width: '100%',
        height: '100%',
        padding: 0,
        backgroundImage:bgDefault
    },
    shapeDefaults: {
        id: "",
        name: "",
        title: "",
        samplePictures: "",    // 显示的组件图标
        componentHtml: "",     // 实际在操作区域显示的组件
        computerType: "",    // 组件选择的类型
        computerStyle: "",
        imgData: {},
        nodel: true,    // 是否启用快捷删除组件功能
        componentsObj: {},    // 组件集合
        componentid: 0,    // 模板的ID
        componenturl: "",    // 模板的地址
        ajaxUrl: "",    // ajax地址
        scriptName: "",    // js代码名称
        maxlen: 0,     // 组件最大数量
        category: "",
        group: "",
        groupName: null,
        locked: false,
        link: "",
        children: [],
        parent: "",
        resizeDir: ["tl", "tr", "br", "bl"],
        attribute: {
            container: false,
            visible: true,
            rotatable: true,
            linkable: true,
            collapsable: false,
            collapsed: false,
            markerOffset: 5
        },
        dataAttributes: [],
        props: {x: 0, y: 0, w: 120, h: 80, zindex: 0, angle: 0},
        shapeStyle: {alpha: 1},
        lineStyle: {lineWidth: 2, lineColor: "50,50,50", lineStyle: "solid"},
        fillStyle: {type: "solid", color: "255,255,255"},
        path: [{
            actions: [
                {action: "move", x: "0", y: "0"},
                {action: "line", x: "w", y: "0"},
                {action: "line", x: "w", y: "h"},
                {action: "line", x: "0", y: "h"},
                {action: "close"}
            ]
        }],
        fontStyle: {
            fontFamily: "微软雅黑",
            size: 12,
            color: "50,50,50",
            bold: false,
            italic: false,
            underline: false,
            textAlign: "center",
            vAlign: "middle"
        },
        anchors: [ //锚
            {x: "w/2", y: "0"},
            {x: "w/2", y: "h"},
            {x: "0", y: "h/2"},
            {x: "w", y: "h/2"}
        ]
    },
    categories: [],//组件类别数组
    shapes: {},
    addCategory: function (a) {//添加组件类别
        Schema.categories.push(a);
        CategoryMapping[a.name] = a;
    },
    addShape: function (a) {
        if (typeof Schema.shapes[a.name] != "undefined") {
            throw"--Duplicated shape name: " + a.name;
        }
        if (a.groupName) {
            SchemaGroup.addGroupShape(a.groupName, a.name);
        }
        Schema.shapes[a.name] = this.initShape(a);
    },
    initShape: function (b) {
        var a = {};
        for (var c in this.shapeDefaults) {
            if (c == "attribute") {
                a.attribute = this.extend(b.attribute, this.shapeDefaults.attribute);
            } else {
                if(c === "componenturl") {
                    a.componenturl = b.componenturl;
                }
                if(c === "maxlen") {
                    a.maxlen = b.maxlen;
                }
                if(c === "componentHtml") {
                    a.componentHtml = b.componentHtml;
                }
                if(c === "ajaxUrl") {
                    a.ajaxUrl = b.ajaxUrl;
                }
                if(c === "scriptName") {
                    a.scriptName = b.scriptName;
                }

                if(c === "componentid") {
                    a.componentid = b.componentid;
                }
                if(c === "samplePictures") {
                    a.samplePictures = b.samplePictures;
                }
                if(c === "computerType") {
                    a.computerType = b.computerType;
                }
                if(c === "computerStyle") {
                    a.computerStyle = b.computerStyle;
                }
                if(c === "imgData") {
                    a.imgData = b.imgData;
                }
                if(c === "nodel") {
                    a.nodel = b.nodel;
                }
                if(c === "componentsObj") {
                    a.componentsObj = b.componentsObj;
                }
                if (c == "props") {
                    a.props = this.extend(b.props, this.shapeDefaults.props);
                } else {
                    if (c == "shapeStyle") {
                        a.shapeStyle = this.extend(b.shapeStyle, this.shapeDefaults.shapeStyle);
                    } else {
                        if (c == "lineStyle") {
                            a.lineStyle = this.extend({}, b.lineStyle)
                        } else {
                            if (c == "fillStyle") {
                                a.fillStyle = this.extend({}, b.fillStyle)
                            } else {
                                if (c == "fontStyle") {
                                    a.fontStyle = this.extend({}, b.fontStyle)
                                } else {
                                    if (typeof b[c] != "undefined") {
                                        a[c] = b[c];
                                    } else {
                                        a[c] = this.shapeDefaults[c];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if (b.onCreated) {
            a.onCreated = b.onCreated;
        }
        if (b.drawIcon) {
            a.drawIcon = b.drawIcon;
        }
        return a;
    },
    extend: function (d, c) {
        if (!d) {
            d = {};
        }
        var a = {};
        for (var b in c) {
            a[b] = c[b];
        }
        for (var b in d) {
            a[b] = d[b];
        }
        return a;
    },
    addGlobalCommand: function (a, b) {
        GlobalCommand[a] = b
    },
    empty: function () {
        Schema.categories = [];
        Schema.shapes = {};
        CategoryMapping = {};
        SchemaGroup.groups = {}
    },
    init: function (c) {
        for (var b in Schema.shapes) {
            var a = Schema.shapes[b];
            this.initShapePath(a);
            if (c) {
                this.initShapeFunctions(a);
            }
            this.initShapeDataAttribute(a);
        }
    },
    initShapePath: function (a) {
        if (a.path) {
            for (var b = 0; b < a.path.length; b++) {
                var c = a.path[b];
                if (c.actions && c.actions.ref) {
                    a.path[b].actions = GlobalCommand[c.actions.ref];
                }
            }
        }
    },
    initShapeFunctions: function (shape) {
        var pathEval = "shape.getPath = function(){var color = [255,255,255];var fillStyle = Utils.getShapeFillStyle(this.fillStyle, false);if(fillStyle.color && fillStyle.color.length > 0){color = fillStyle.color.split(',');}var r = color[0]; var g = color[1]; var b = color[2];var w = this.props.w; var h = this.props.h;var lineStyle = Utils.getShapeLineStyle(this.lineStyle, false); var lineWidth = lineStyle.lineWidth; ";
        pathEval += SchemaHelper.constructPathFunBody(shape.path) + "}";
        eval(pathEval);
        var anchorsEval = "shape.getAnchors = function(){var w = this.props.w; var h = this.props.h; return [";
        for (var i = 0; i < shape.anchors.length; i++) {
            var anchor = shape.anchors[i];
            anchorsEval += "{x:" + anchor.x + ", y:" + anchor.y + "}";
            if (i < shape.anchors.length - 1) {
                anchorsEval += ",";
            }
        }
        anchorsEval += "];}";
        eval(anchorsEval);
    },
    initShapeDataAttribute: function (b) {
        var a = CategoryMapping[b.category].dataAttributes;
        if (!b.dataAttributes) {
            b.dataAttributes = [];
        }
        if (a && a.length > 0) {
            b.dataAttributes = a.concat(b.dataAttributes);
        }
    }
};
Schema.addGlobalCommand("rectangle", [
    {action: "move", x: "0", y: "0"},
    {action: "line", x: "w", y: "0"},
    {action: "line", x: "w", y: "h"},
    {action: "line", x: "0", y: "h"},
    {action: "close"}
]);
var SchemaHelper = {
    constructPathFunBody: function (n) {
        var d = "return [";
        for (var k = 0; k < n.length; k++) {
            var c = n[k];
            d += "{";
            if (c.fillStyle) {
                var a = "fillStyle: {";
                var m = c.fillStyle;
                if (typeof m.type != "undefined") {
                    a += "type:'" + m.type + "',";
                }
                if (typeof m.color != "undefined") {
                    var l = m.color.split(",");
                    var f = "";
                    if (l[0].indexOf("r") >= 0) {
                        f += "(" + l[0] + ")+','+";
                    } else {
                        f += "'" + l[0] + ",'+";
                    }
                    if (l[1].indexOf("g") >= 0) {
                        f += "(" + l[1] + ")+','+";
                    } else {
                        f += "'" + l[1] + ",'+";
                    }
                    if (l[2].indexOf("b") >= 0) {
                        f += "(" + l[2] + ")";
                    } else {
                        f += "'" + l[2] + "'";
                    }
                    a += "color:" + f + ",";
                }
                if (typeof m.angle != "undefined") {
                    a += "angle:" + m.angle + ",";
                }
                if (typeof m.display != "undefined") {
                    a += "display:'" + m.display + "',";
                }
                if (typeof m.alpha != "undefined") {
                    a += "alpha:" + m.alpha + ",";
                }
                a = a.substring(0, a.length - 1);
                a += "},";
                d += a;
            }
            if (c.lineStyle) {
                var b = "lineStyle: {";
                if (typeof c.lineStyle.lineWidth != "undefined") {
                    b += "lineWidth:" + c.lineStyle.lineWidth + ",";
                }
                if (typeof c.lineStyle.lineStyle != "undefined") {
                    b += "lineStyle:'" + c.lineStyle.lineStyle + "',";
                }
                if (typeof c.lineStyle.lineColor != "undefined") {
                    b += "lineColor:'" + c.lineStyle.lineColor + "',";
                }
                b = b.substring(0, b.length - 1);
                b += "},";
                d += b;
            }
            d += "actions:[";
            var h = c.actions;
            for (var g = 0; g < h.length; g++) {
                var e = h[g];
                d += "{action:'" + e.action + "'";
                if (e.action == "move" || e.action == "line") {
                    d += ",x:" + e.x + ",y:" + e.y;
                } else {
                    if (e.action == "curve") {
                        d += ",x1:" + e.x1 + ",y1:" + e.y1 + ",x2:" + e.x2 + ",y2:" + e.y2 + ",x:" + e.x + ",y:" + e.y;
                    } else {
                        if (e.action == "quadraticCurve") {
                            d += ",x1:" + e.x1 + ",y1:" + e.y1 + ",x:" + e.x + ",y:" + e.y;
                        }
                    }
                }
                d += "}";
                if (g < h.length - 1) {
                    d += ",";
                }
            }
            d += "]}";
            if (k < n.length - 1) {
                d += ",";
            }
        }
        d += "];";
        return d;
    }
};
var SchemaGroup = {
    groups: {},
    addGroupShape: function (b, a) {
        if (!this.groupExists(b)) {
            this.groups[b] = [a];
        } else {
            this.groups[b].push(a);
        }
    },
    groupExists: function (a) {
        if (this.groups[a]) {
            return true;
        } else {
            return false;
        }
    },
    getGroup: function (a) {
        return this.groups[a];
    }
};